package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountUser;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.Constants.UserType;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 创建护理组
 * 
 * @author li.li
 *
 */
public class AccountAddUserTypeFragment extends BaseProgressFragment {

	@Bind(R.id.id_account_portrait)
	protected ImageView portrait;

	@Bind(R.id.id_account_name)
	protected EditText nameText;

	@Bind(R.id.id_account_user_type)
	protected TextView userTypeText;
	
	//当前选择的类型
	private UserType selectUserType;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_add_user_type;
	}

	@Override
	protected void initParams() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initData() {

	}

	@OnClick(R.id.id_account_submit_btn)
	public void submit() {
		
		if(StringUtils.isBlank(nameText.getText().toString())){
			ViewUtils.toast("用户名不能为空");
			return;
		}
		
		if(selectUserType == null){
			ViewUtils.toast("用户名身份不能为空");
			return;
		}

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

		if (tokenKeeper.isLogin()) {
			
			//保存用户身份在本地
			AccountUser user = tokenKeeper.getUser();
			user.setUserType(selectUserType.getValue());
			AccessTokenKeeper.keepAccessToken(getActivity(), tokenKeeper);
			
			RequestParams params = new RequestParams();
			params.put("uid", tokenKeeper.getOpenId());
			params.put("user_name", nameText.getText().toString());
			params.put("user_type", selectUserType.getValue());
			ApiRequest.accountAddUserType.request(this, params);
		}

	}
	
	@OnClick(R.id.id_account_user_type)
	public void userType() {
		AccountTypeAddDialogFragment accountTypeAddDialogFrag = new AccountTypeAddDialogFragment();
		accountTypeAddDialogFrag.show(getChildFragmentManager(), accountTypeAddDialogFrag.getClass().getSimpleName());
	}
	
	public UserType getSelectUserType() {
		return selectUserType;
	}

	public void setSelectUserType(UserType selectUserType) {
		this.selectUserType = selectUserType;
	}
	
	public TextView getUserTypeText() {
		return userTypeText;
	}

	public void setUserTypeText(TextView userTypeText) {
		this.userTypeText = userTypeText;
	}

	public static void go(Activity act) {
		MyPageHelper.accountAddUserType.showMyPage(act);
	}
	
	

}
